package com.yxhl.zoume.core.func.push;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.yxhl.protobuf.OrderBizType;
import com.yxhl.zoume.UIConstants;
import com.yxhl.zoume.core.expresscar.activity.ExpressCarScheduleActivity;
import com.yxhl.zoume.core.func.push.info.PushActionType;
import com.yxhl.zoume.core.func.push.model.PushMessage;
import com.yxhl.zoume.core.main.info.MainEntrance;
import com.yxhl.zoume.core.main.model.MainEntranceInfo;
import com.yxhl.zoume.core.main.ui.MainActivity;
import com.yxhl.zoume.core.specialcar.activity.SpecialCarScheduleActivity;
import com.yxhl.zoume.core.tripsmgmt.ui.activity.TripDetailContainerActivity;
import com.yxhl.zoume.utils.ActivityControlUtils;
import com.yxhl.zoume.utils.LOG;

/**
 * 消息点击处理类
 * Created by vhawk on 16/3/15.
 */
public class PushMessageDispatcher {

    private static final String TAG = "PushMessageDispatcher";

    public static PushMessageDispatcher instance;

    private PushMessageDispatcher() {
    }

    public static PushMessageDispatcher getInstance() {
        if (instance == null) {
            synchronized (PushMessageDispatcher.class) {
                if (instance == null) {
                    instance = new PushMessageDispatcher();
                }
            }
        }
        return instance;
    }

    public void onClick(Context context, PushMessage pushMessage) {
        if (pushMessage == null) {
            return;
        }
        LOG.e(TAG, "pushMessage=" + pushMessage);
        Intent intent;
        String action = pushMessage.getAction();
        boolean isAppRunning = ActivityControlUtils.contains(MainActivity.class);
        LOG.e(TAG, "isAppRunning=" + isAppRunning);
        if (isAppRunning) {
            switch (action) {
                case PushActionType.TRIP_DETAIL:
                    intent = TripDetailContainerActivity.getPushIntent(context, pushMessage);
                    context.startActivity(intent);
//                    int bizType = pushMessage.getBizType();
//                    switch (bizType) {
//                        //新地巴士
//                        case PushBizType.TICKET:
//
//                            break;
//                        // 快车
//                        case PushBizType.SHARE:
//                        case PushBizType.CHARTER:
//                        //定制巴士
//                        case PushBizType.DIY_BUS:
//                        //固定时刻巴士
//                        case PushBizType.FT_BUS:
//                            break;
//                    }
                    break;
                case PushActionType.CURRENT_TRIP:
                case PushActionType.OPEN_APP:
                    MainEntranceInfo<PushMessage> mainEntranceInfo =
                            new MainEntranceInfo<>(MainEntrance.PUSH);
                    mainEntranceInfo.getEntranceMap().put(MainEntrance.PUSH, pushMessage);

                    intent = MainActivity.getPushIntent(context, mainEntranceInfo);
                    context.startActivity(intent);
                    break;
                case PushActionType.SPECIAL_CAR_SCHEDULE_MAP:
                    String orderId = pushMessage.getId();
                    int bizType = pushMessage.getBizType();
                    if (!TextUtils.isEmpty(orderId)) {
                        switch (bizType) {
                            case OrderBizType.SPECIAL_BUS_WAY_VALUE:
                            case OrderBizType.SPECIAL_BUS_TIME_VALUE:
                                intent = ExpressCarScheduleActivity.getPushIntent(context,orderId);
                                context.startActivity(intent);
                                return;
                        }
                        intent = SpecialCarScheduleActivity.getPushIntent(context, orderId);
                        context.startActivity(intent);
                    }
                    break;
            }
        } else {
            Intent launchIntentForPackage = context.getPackageManager()
                    .getLaunchIntentForPackage(context.getPackageName());

            MainEntranceInfo<PushMessage> mainEntranceInfo = new MainEntranceInfo<>(MainEntrance.PUSH);
            mainEntranceInfo.getEntranceMap().put(MainEntrance.PUSH, pushMessage);

            launchIntentForPackage.putExtra(UIConstants.Push.INTENT_KEY_PUSH_INFO, mainEntranceInfo);

            context.startActivity(launchIntentForPackage);
        }
    }
}
